import { createRouter, createWebHistory } from 'vue-router'
import VueCookies from 'vue-cookies'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '首页',
      component:() => import("@/views/Index.vue"),
      children: [
        {
          path: "/",
          name: "首页",
          meta: {
            needLogin: false,
            menuCode: "main"
          },
          component: () => import("@/views/Framework.vue")
        },
        {
          path: "/case",
          name: "作品展示页",
          meta: {
            needLogin: false,
            menuCode: "case"
          },
          component: () => import("@/views/Case.vue")
        },
        {
          path: "/caseDetail/:id",
          name: "作品详情页",
          meta: {
            needLogin: false,
            menuCode: "caseDetail"
          },
          component: () => import("@/views/CaseDetail.vue")
        },
        {
          path: "/service",
          name: "业务",
          meta: {
            needLogin: false,
            menuCode: "service"
          },
          component: () => import("@/views/Service.vue")
        },
        {
          path: "/about",
          name: "关于",
          meta: {
            needLogin: false,
            menuCode: "about"
          },
          component: () => import("@/views/About.vue")
        },
        {
          path: "/idea",
          name: "观点",
          meta: {
            needLogin: false,
            menuCode: "idea"
          },
          component: () => import("@/views/Idea.vue")
        },
        {
          path: "/ideaDetail/:id",
          name: "观点详情页",
          meta: {
            needLogin: false,
            menuCode: "ideaDetail"
          },
          component: () => import("@/views/IdeaDetail.vue")
        },
        {
          path: "/contact",
          name: "联系",
          meta: {
            needLogin: false,
            menuCode: "contact"
          },
          component: () => import("@/views/Contact.vue")
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo");
  if (to.meta.needLogin != null && to.meta.needLogin && userInfo == null) {
    router.push("/login")
  }
  next();
})

export default router
